# Maintainer: Miloš Komarčević <miloskomarcevic@aim.com>

_realname=include-what-you-use
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=0.23
pkgrel=2
pkgdesc="A tool for use with clang to analyze #includes in C and C++ source files (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='http://include-what-you-use.org'
msys2_repository_url="https://github.com/include-what-you-use/include-what-you-use"
msys2_references=(
  'aur: include-what-you-use'
)
license=('spdx:NCSA')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-clang-libs"
         "${MINGW_PACKAGE_PREFIX}-llvm-libs"
         "${MINGW_PACKAGE_PREFIX}-python")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-clang-tools-extra")
source=("https://github.com/${_realname}/${_realname}/archive/${pkgver}/${_realname}-${pkgver}.tar.gz"
        "001-fix-build-with-llvm-20.patch::https://github.com/include-what-you-use/include-what-you-use/commit/e2e42c6b.patch")
sha256sums=('16672743fdf781d7db313f48c7f4b34bc6517eddfd92d7db793790fe793a306c'
            'ed0cdfa8c6e16c0c20346ffcf7c1fb31b58c85f1bf1308976b5c0132a1a17c4b')

prepare() {
  cd ${_realname}-${pkgver}
  patch -p1 -i "${srcdir}"/001-fix-build-with-llvm-20.patch
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      -DCMAKE_INSTALL_LIBDIR=lib \
      "${extra_config[@]}" \
      -DPython3_EXECUTABLE=${MINGW_PREFIX}/bin/python \
      ../${_realname}-${pkgver}

  "${MINGW_PREFIX}"/bin/cmake.exe --build .
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE.TXT" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
